<?php
class Contents extends Public_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	
	function index($category_id)
	{
		if($category_id==43)
		{
			redirect('boards/index/main/1');
		}
		if($category_id==51)$category_id = 64;
		if($category_id==49)$category_id = 56;
		if($category_id==48)$category_id = 60;
		$root_id = get_root($category_id);
		$root = new category($root_id);
		$data['slug'] = $root->slug;
		$data['category'] = new category($category_id);
		if($data['category']->structure->type=='list')
		{
			$data['contents'] = new content();
			$group[18] = '18,154,122';
			$group[19] = '19,123,155,220';
			$group[20] = '20,156,125';
			$group[21] = '21,157,127,228';
			$group[22] = '22,158';
			$group[23] = '23,159,332';
			$group[11] = '11,334,61,62,63';
			
			$group[300] = '300,154,155,156,157,158,159,160';
			$group[301] = '301,220,221,222,224,226,228,229,230';
			$group[302] = '302,122,123,124,125,127,128,129,130,332';
			if(!empty($group[$category_id]))
			{
				$data['contents']->where('category_id in ('.$group[$category_id].')');
			}
			elseif($category_id==9)
			{
				$data['contents']->where("category_id in (select id from content_categories where layout_id = 12)");
			}
			else
			{
				$data['contents']->where("category_id = ".$category_id);
			}
			$data['contents']->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'");
			
			
			
			if(@$_GET['search'])$data['contents']->where("title like '%".$_GET['search']."%'");
			if($category_id==4)
			{
				if(@$_GET['news_paper_id'])$data['contents']->where('news_paper_id',$_GET['news_paper_id']);
				if(@$_GET['news_type_id'])$data['contents']->where('news_type_id',$_GET['news_type_id']);
				if(@$_GET['news_date'])$data['contents']->where('news_date',Date2DB($_GET['news_date']));
				$data['contents']->order_by('news_date','desc');
			}
			$data['contents']->order_by('id','desc')->get_page(10);
			$this->template->append_metadata(js_datepicker());
			$this->template->build('content_index',$data);
		}
		elseif($data['category']->structure->type=='organization')
		{
			$this->template->build('content_organization',$data);
		}
		elseif($data['category']->structure->type=='agencies')
		{
			$data['agencies'] = new category;
			$data['agencies']->where('structure_id',1)->get();
			$this->template->build('content_agencies',$data);
		}
		elseif($data['category']->structure->type=='download')
		{
			$data['contents'] = new content();
			$data['contents']->where("category_id = ".$category_id);
			$data['contents']->where("status = 'approve'");
			$data['contents']->where("file <> ''");
			if(@$_GET['search'])$data['contents']->where("title like '%".$_GET['search']."%'");
			$data['contents']->order_by('id','desc')->get_page(10);
			$this->template->build('content_download',$data);
		}
		elseif($data['category']->structure->type=='category')
		{
			$data['categories'] = new Category();
			$data['categories']->where('parent_id',$category_id)->get();
			$data['parent'] = new category($category_id);
			if($category_id==26)
			{
				$this->template->build('content_category_download',$data);
			}
			else
			{
				$this->template->build('content_category',$data);
			}
			
		}
		elseif($data['category']->structure->type=='contact')
		{
			$data['content'] = new content();
			$data['content']->where('category_id',$category_id)->get();
			$this->template->build('content_contact',$data);
		}
		elseif($data['category']->structure->type=='page')
		{
			$data['content'] = new content();
			$data['content']->where('category_id',$category_id)->get();
			if($data['content']->exists())
			{
				$this->template->build('content_page',$data);
			}
			else
			{
				$this->template->build('content_blank',$data);
			}
			
		}
		elseif($data['category']->structure->type=='board')
		{
			redirect('boards/index/main/2');
		}
	}
	
	function search($slug = 'main')
	{
		if(!empty($_GET['search']))
		{
			$keyword = new Keyword;
			$keyword->get_by_name($_GET['search']);
			if($keyword->exists())
			{
				$keyword->counter();
			}
			$keyword->name = $_GET['search'];
			$keyword->save();
		}
		$data['slug'] = $slug;
		$data['contents'] = new Content;
		$data['contents']->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'");
		$data['contents']->like('title',$_GET['search']);
		$data['contents']->order_by('id','desc')->get_page();
		$this->template->build('content_search',$data);
	}
	
	function tag($slug = 'main')
	{
		$data['slug'] = $slug;
		$data['contents'] = new Content;
		$data['contents']->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'");
		$data['contents']->like('tag',$_GET['tag']);
		$data['contents']->order_by('id','desc')->get_page();
		$this->template->build('content_tag',$data);
	}
	
	function inc_index($category_id)
	{
		$data['contents'] = new Content;
		$data['contents']->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'");
		$data['contents']->where('category_id',$category_id)->order_by('id','desc')->limit(5)->get();
		$this->load->view('inc_index',$data);
	}
	
	function inc_marquee($category_id)
	{
		$data['content'] = new content();
		$data['content']->where('category_id',$category_id)->get();
		echo lang_decode($data['content']->detail);
	}
	
	function inc_thumbnail($category_id)
	{
		$data['contents'] = new Content;
		$data['contents']->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'");
		$data['contents']->where('category_id',$category_id)->order_by('id','desc')->limit(7)->get();
		$this->load->view('inc_thumbnail',$data);
	}
	
	function view($id)
	{
		$data['content'] = new Content($id);
		$group[11] = TRUE;
		$group[334] = TRUE;
		$group[61] = TRUE;
		$group[62] = TRUE;
		$group[63] = TRUE;
		if((@$group[$data['content']->category_id])&&(!is_login()))
		{
			//set_notify('error', 'กรุณาเข้าสู่ระบบก่อนเข้าใช้งานในส่วนนี้');
			header ('Content-type: text/html; charset=utf-8');
			echo '
				<script type="text/javascript">
				alert("กรุณาเข้าสู่ระบบก่อนเข้าใช้งานในส่วนนี้");
				window.location = "'.$_SERVER['HTTP_REFERER'].'";
				</script>
				';
			//redirect($_SERVER['HTTP_REFERER']);
			exit();
		}
		$root_id = get_root($data['content']->category_id);
		$root = new category($root_id);
		$data['slug'] = $root->slug;
		$data['content']->counter();
		/*if($data['content']->file)
		{
			redirect('uploads/content/download/'.$data['content']->file);
			exit();
		}*/
		$this->template->build('content_view',$data);
	}
	
	public function send($id)
	{
		if($_POST['captcha'] === $this->session->userdata('captcha'))
		{
			$content = new Content($id);
			$this->load->library('email');
			$this->email->from($_POST['email'], $_POST['name']);
			$this->email->to($content->email);
			$this->email->subject($_POST['title']);
			$this->email->message($_POST['detail']);
			if($this->email->send())
			{
				set_notify('success', 'Send email complete');
			}
			else
			{
				set_notify('success', 'Cannot send email');
			}
		}
		redirect($this->agent->referrer());
	}
	
	function download($id)
	{
		$content = new Content($id);
		$content->counter();
		$this->load->helper('download');
		$data = file_get_contents("uploads/content/download/".basename($content->file));
		$name = basename($content->file);
		force_download($name, $data); 
	}
	
	function inc_information()
	{
		$data['contents'] = new Content;
		$data['contents']->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'");
		$data['contents']->where('category_id',1)->order_by('id','desc')->limit(4)->get();
		$this->load->view('inc_information',$data);
	}
	
	function inc_work()
	{
		$data['contents'] = new Content;
		$data['contents']->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'");
		$data['contents']->where('category_id',3)->order_by('id','desc')->limit(3)->get();
		$this->load->view('inc_work',$data);
	}
	
	function inc_meeting()
	{
		$data['contents'] = new Content;
		$data['contents']->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'");
		$data['contents']->where('category_id',2)->order_by('id','desc')->limit(3)->get();
		$this->load->view('inc_meeting',$data);
	}
	
	function inc_tab($category_id)
	{
		$data['category_id'] = $category_id;
		$data['categories'] = new Category();
		$data['categories']->where('parent_id',$category_id)->order_by('id','asc')->get();
		$this->load->view('inc_tab',$data);
	}
	
	function rss()
	{
		$data['contents'] = new Content;
		$data['contents']->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'");
		$data['contents']->where_related('category/layout','name','information')->order_by('id','desc')->limit(20)->get();
		$this->load->view('rss',$data);
	}
	
	public function keyword()
	{
		$comma = '';
		$keywords = new Keyword;
		$keywords->get();
		foreach($keywords as $keyword)
		{
			echo $comma.'"'.$keyword->name.'"';
			$comma = ',';
		}
	}
}
?>